package com.liuyun.unixsocket;

import jnr.unixsocket.UnixServerSocketChannel;
import jnr.unixsocket.UnixSocketChannel;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

/**
 * @author liuyun
 */
public class ServerActor implements Actor {
    private final UnixServerSocketChannel channel;
    private final Selector selector;

    public ServerActor(UnixServerSocketChannel channel, Selector selector) {
        this.channel = channel;
        this.selector = selector;
    }

    @Override
    public final boolean hasReady() {
        try {
            UnixSocketChannel client = channel.accept();
            client.configureBlocking(false);
            client.register(selector, SelectionKey.OP_READ, new ClientActor(client));
            return true;
        } catch (IOException ex) {
            return false;
        }
    }
}
